package com.huanshi.module.pms.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huanshi.common.CommonResult;
import com.huanshi.entity.PmsProduct;
import com.huanshi.module.pms.vo.PmsProductDetailVO;
import com.huanshi.service.PmsProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * spu描述的是一个抽象性的商品，比如 iphone8Controller
 * 
 * @author huanshi
 * @date 2023-07-02
 */
@RestController
@RequestMapping("/api/pms/product")
public class PmsProductController {
    @Autowired
    private PmsProductService productService;

    /**
     * 查询spu描述的是一个抽象性的商品，比如 iphone8列表
     */

    @GetMapping("/list")
    public CommonResult<IPage<PmsProductDetailVO>> list(
            @RequestParam(value = "productName", defaultValue = "") String productName,
            @RequestParam(value = "categoryId", defaultValue = "") Integer categoryId,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {

        return CommonResult.success(productService.findPage(new Page<>(pageNum, pageSize), categoryId, productName));
    }


    @GetMapping("/recommend")
    public CommonResult<IPage<PmsProductDetailVO>> getRecommend(
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        return CommonResult.success(productService.findRecommendPage(new Page<>(pageNum, pageSize)));
    }

    /**
     * 获取spu描述的是一个抽象性的商品，比如 iphone8详细信息
     */
    @GetMapping(value = "/{id}")
    public CommonResult<PmsProduct> getInfo(@PathVariable("id") Long id) {
        return CommonResult.success(productService.getById(id));
    }

    /**
     * 新增spu描述的是一个抽象性的商品，比如 iphone8
     */
    @PostMapping
    public CommonResult<Boolean> update(@RequestBody PmsProduct product)
    {
        return CommonResult.success(productService.saveOrUpdate(product));
    }

    /**
     * 修改spu描述的是一个抽象性的商品，比如 iphone8
     */
    @PutMapping
    public CommonResult<Boolean> edit(@RequestBody PmsProduct spu)
    {
        return CommonResult.success(productService.updateById(spu));
    }

    /**
     * 删除spu描述的是一个抽象性的商品，比如 iphone8
     */
	@DeleteMapping("/del")
    public CommonResult<Boolean> remove(@RequestParam(name = "id") Integer id) {
        return CommonResult.success(productService.removeById(id));
    }
}
